package com.eric.server.logic;

import com.eric.server.logic.packet.ChatToGroupPacket;
import com.eric.server.logic.packet.NoticePacket;
import com.eric.server.logic.packet.UserOutPacket;
import com.eric.server.monitor.MonitorListener;
import io.netty.channel.Channel;

import java.util.*;

/**
 * Created by eric on 2017/11/16.
 */
public class DefaultLogic extends AbstractLogic {

    private static DefaultLogic instance ;

    public static DefaultLogic me(){
        if(instance == null){
            instance = new DefaultLogic();
        }
        return instance;
    }

    Map<Channel,IoSession>  channelIoSessionMap = new HashMap<Channel, IoSession>();
    Map<String,List<Channel>> roomChannelMap = new HashMap<String, List<Channel>>();
    MonitorListener listener ;

    public void setListener(MonitorListener listener){
        this.listener = listener;
    }

    @Override
    public void createRoom(String roomName) {
        super.createRoom(roomName);
        roomChannelMap.put(roomName, new ArrayList<Channel>() );
    }

    @Override
    public void join(Channel channel, String user, String room) {
        super.join(channel, user, room);
        IoSession session = new IoSession(channel);
        session.setRoom(room);
        session.setUser(user);
        channelIoSessionMap.put(channel,session);
        List<Channel>channels = roomChannelMap.get(room);
        if(channels == null){
            channel.writeAndFlush(NoticePacket.obtain("login_fail"));
        }else{
            channel.writeAndFlush(NoticePacket.obtain("login_ok"));

            noticeJoinRoom(room,user);
            channels.add(channel);
        }
        if(listener != null)
            listener.notice(user+" 进入 "+room);
    }

    @Override
    public void leave(Channel channel) {
        super.leave(channel);
        IoSession session = channelIoSessionMap.get(channel);
        if(session == null)return ;
        List<Channel> channels = roomChannelMap.get(session.getRoom());
        if(channels != null){
            channels.remove(channel);
        }
        channelIoSessionMap.remove(channel);
        noticeLeave(session.getRoom(),session.getUser());
        if(listener != null)
            listener.notice(session.getUser()+" 离开 "+session.getRoom());
    }

    @Override
    public void sendMsg(Channel channel, String msg) {
        super.sendMsg(channel, msg);
        IoSession session = channelIoSessionMap.get(channel);
        sendMsgToRoom(session.getUser(),session.getRoom(),msg);
        if(listener != null)
            listener.notice(session.getUser()+" 说: "+msg);
    }

    public void noticeJoinRoom(String room,String user){
        List<Channel> channels = roomChannelMap.get(room);
        NoticePacket packet = NoticePacket.obtain(" >> 欢迎 "+user+" 进入群聊 << ");
        for (Channel channel : channels){
            channel.writeAndFlush(packet);
        }
    }

    public void noticeLeave(String room,String user){
        List<Channel> channels = roomChannelMap.get(room);
        NoticePacket packet = NoticePacket.obtain(" >> "+user +"  离开  << ");
        for (Channel channel : channels){
            channel.writeAndFlush(packet);
        }
    }

    public void sendMsgToRoom(String user,String room,String msg){
        List<Channel> channels = roomChannelMap.get(room);
        ChatToGroupPacket packet = (ChatToGroupPacket)ChatToGroupPacket.obtain(user,room,msg);
        for (Channel channel : channels){
            channel.writeAndFlush(packet);
        }
    }
}
